package ar.edu.itba.pod.main;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;

import org.joda.time.Duration;

import ar.edu.itba.balance.api.NotCoordinatorException;
import ar.edu.itba.pod.agent.market.Consumer;
import ar.edu.itba.pod.agent.market.Market;
import ar.edu.itba.pod.agent.market.Producer;
import ar.edu.itba.pod.agent.market.Resource;
import ar.edu.itba.pod.legajo45129.agentSimulation.DistributedSimulation;
import ar.edu.itba.pod.legajo45129.manager.NodeManager;
import ar.edu.itba.pod.time.TimeMapper;
import ar.edu.itba.pod.time.TimeMappers;

public class Test {
	private static Resource gold = SimulationFactory.getGold();

	private static final int portA = 9090;
	private static final int portB = 9091;
	private static final int portC = 9092;
	
	public static void A() throws RemoteException, NotCoordinatorException, InterruptedException {
		String host = "localhost";
		
		TimeMapper timeMapper = TimeMappers.oneSecondEach(Duration.standardHours(24));
		NodeManager node = NodeManager.getInstance();
		DistributedSimulation simulation = new DistributedSimulation(timeMapper);
		node.configure(host, portA, "A", simulation);
		node.getClusterAdministration().createGroup();
		simulation.add(new Market("gold market", gold));
		simulation.startAndWait(Duration.standardMinutes(1));
	}
	
	
	public static void B() throws InterruptedException, RemoteException, NotBoundException, NotCoordinatorException {
		String host = "localhost";
		TimeMapper timeMapper = TimeMappers.oneSecondEach(Duration.standardHours(24));
		NodeManager node = NodeManager.getInstance();
		DistributedSimulation simulation = new DistributedSimulation(timeMapper);
		node.configure(host, portB, "B", simulation);
		node.getClusterAdministration().connectToGroup(host, portA); //Aca se conecta al nodo A
		simulation.add(new Producer(portB + " - gold mine " + portA, gold, Duration.standardDays(1), 12));
		simulation.add(new Consumer(portB + " - gold consumer 1 " + portA, gold, Duration.standardDays(1), 4));
		simulation.add(new Consumer(portB + " - gold consumer 2 " + portA, gold, Duration.standardDays(1), 4));
		simulation.add(new Consumer(portB + " - gold consumer 3 " + portA, gold, Duration.standardDays(1), 4));

		simulation.startAndWait(Duration.standardMinutes(1));
		return;
	}
	
	public static void C() throws InterruptedException, RemoteException, NotBoundException, NotCoordinatorException {
		String host = "localhost";
		TimeMapper timeMapper = TimeMappers.oneSecondEach(Duration.standardHours(24));
		NodeManager node = NodeManager.getInstance();
		DistributedSimulation simulation = new DistributedSimulation(timeMapper);
		node.configure(host, portC, "C", simulation);
		node.getClusterAdministration().connectToGroup(host, portA); //Aca se conecta al nodo A
		simulation.add(new Producer(portC + " - gold mine " + portA, gold, Duration.standardDays(1), 12));
		simulation.add(new Consumer(portC + " - gold consumer 1 " + portA, gold, Duration.standardDays(1), 4));
		simulation.add(new Consumer(portC + " - gold consumer 2 " + portA, gold, Duration.standardDays(1), 4));
		simulation.add(new Consumer(portC + " - gold consumer 3 " + portA, gold, Duration.standardDays(1), 4));

		simulation.startAndWait(Duration.standardMinutes(1));
		return;
	}




}
